package offer;

/**
 * <p>
 * 剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
 * </p>
 *
 * @author xiaozuolin
 * @date 2021/9/23 14:50
 */
public class Offer21 {
    public static void main(String[] args) {
        int[] nums = exchange(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
        for (int num : nums) {
            System.out.print(num + " ");
        }
    }

    public static int[] exchange(int[] nums) {
        int i = 0;
        int j = nums.length - 1;
        int tmp;
        while (i < j) {
            while (i < j && (nums[i] & 1) == 1) {
                i++;
            }
            while (i < j && (nums[j] & 1) == 0) {
                j--;
            }
            tmp = nums[i];
            nums[i] = nums[j];
            nums[j] = tmp;
        }
        return nums;
    }

}
